package com.slf.engine.biz.impl;

import java.sql.SQLException;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import com.slf.engine.base.IBaseDao;
import com.slf.engine.biz.IFcjs0573Biz;
import com.slf.engine.bo.TFcinfo;
import com.slf.engine.bo.TSys;
import com.slf.engine.common.SysContants;
import com.slf.engine.http.HttpClientManager;
import com.slf.engine.services.IFcjs0573Service;
import com.slf.engine.services.impl.Fcjs0573ServiceImpl;
import com.slf.engine.utils.DateUtils;

public class Fcjs0573BizImpl implements IFcjs0573Biz {

	private static final Logger log = Logger.getLogger(Fcjs0573BizImpl.class);
	private IBaseDao baseDao;
	
	public IBaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(IBaseDao baseDao) {
		this.baseDao = baseDao;
	}

	public boolean addAllFcInfo(HttpClientManager manager, long queryTime,int pageNo)
	{
		try {
			IFcjs0573Service fcjs0573Service = new Fcjs0573ServiceImpl();
			List<TFcinfo> list = fcjs0573Service.getFcInfo(manager, queryTime, pageNo);
			if(pageNo == 1 && list.size() > 0)
			{
				TSys tSys = new TSys();
				tSys.setSysname(SysContants.SYS_FCJS0573_ID);
				tSys.setSysvalue(list.get(0).getFbsj());
				baseDao.modify(tSys, "updateSysValue");
			}
			for(int i = 0; i < list.size(); i++)
			{
				TFcinfo tFcinfo = list.get(i);
				tFcinfo.setRksj(DateUtils.strDate("yyyyMMddHHmmss"));
				baseDao.add(tFcinfo, "addFcInfo");
			}
			if(list.size() == 20)
			{
				pageNo++;
				log.log(Priority.INFO, "开始获取第" + pageNo + "页的数据");
				return addAllFcInfo(manager, queryTime, pageNo); 
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR, "添加数据错误:" + e.getMessage());
			return false;
		}
		return true;
	}
	
	
	public String makeEmailBody(List<TFcinfo> list) throws SQLException
	{
		
		StringBuffer body = new StringBuffer("<html>");
		body.append("<head>");
		body.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
		body.append("<title>");
		body.append("发送时间" + DateUtils.strDate("yyyy-MM-dd HH:mm:ss"));
		body.append("</title>");
		body.append("</head>");
		body.append("<body>");
		body.append("<table width=\"100%\" style=\"text-align:center;\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" bgcolor=\"#222222\">");
		body.append("<tr bgcolor=\"#EEEEEE\">");
		body.append("<td>");
		body.append("信息ID");
		body.append("</td>");
		body.append("<td>");
		body.append("小区路段");
		body.append("</td>");
		body.append("<td>");
		body.append("用途");
		body.append("</td>");
		body.append("<td>");
		body.append("楼层");
		body.append("</td>");
		body.append("<td>");
		body.append("面积");
		body.append("</td>");
		body.append("<td>");
		body.append("汽库");
		body.append("</td>");
		body.append("<td>");
		body.append("自库");
		body.append("</td>");
		body.append("<td>");
		body.append("总价");
		body.append("</td>");
		body.append("<td>");
		body.append("装修");
		body.append("</td>");
		body.append("<td>");
		body.append("发布时间");
		body.append("</td>");
		body.append("<td>");
		body.append("状态");
		body.append("</td>");
		body.append("<td>");
		body.append("联系信息");
		body.append("</td>");
		body.append("<td>");
		body.append("备注");
		body.append("</td>");
		body.append("</tr>");
		for(int i = 0; i < list.size(); i++)
		{
			TFcinfo fcinfo = list.get(i);
			body.append("<tr bgcolor=\"#FFFFFF\">");
			body.append("<td>");
			body.append(fcinfo.getInfoid());
			body.append("</td>");
			body.append("<td>");
			body.append(fcinfo.getXqld());
			body.append("</td>");
			body.append("<td>");
			body.append(fcinfo.getYt());
			body.append("</td>");
			body.append("<td>");
			body.append(fcinfo.getLc());
			body.append("</td>");
			body.append("<td>");
			body.append(fcinfo.getMj());
			body.append("</td>");
			body.append("<td>");
			body.append(fcinfo.getQk());
			body.append("</td>");
			body.append("<td>");
			body.append(fcinfo.getZk());
			body.append("</td>");
			body.append("<td>");
			body.append(fcinfo.getZj());
			body.append("</td>");
			body.append("<td>");
			body.append(fcinfo.getZx());
			body.append("</td>");
			body.append("<td>");
			body.append(fcinfo.getFbsj());
			body.append("</td>");
			body.append("<td>");
			body.append(fcinfo.getZt());
			body.append("</td>");
			body.append("<td>");
			body.append(fcinfo.getLxinfo());
			body.append("</td>");
			body.append("<td>");
			body.append(fcinfo.getBz());
			body.append("</td>");
			body.append("</tr>");
			fcinfo.setFssj(DateUtils.strDate("yyyyMMddHHmmss"));
			baseDao.modify(fcinfo, "hasSend");
		}
		body.append("</table>");
		body.append("</body>");
		body.append("</html>");
		return body.toString();
	}
	
	public String makeEmailTitle(List<TFcinfo> list)
	{
		String title = "";
		for(int i = 0; i < list.size(); i++)
		{
			TFcinfo fcinfo = list.get(i);
			title += "[" + fcinfo.getXqld() + ":" + list.get(i).getLxinfo() + "] ";
		}
		return title;
	}
}
